home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / ctutor.zip / CHAP8.TXT < prev    next >
Text File  |  1987-07-04  |  19KB  |  454 lines

  1.                             Chapter 8 - Pointers
  2.  
  3.  
  4.                              WHAT IS A POINTER?
  5.  
  6.              Simply  stated,  a pointer is an address.   Instead  of
  7.         being  a  variable,  it  is a pointer to a  variable  stored
  8.         somewhere in the address space of the program.  It is always
  9.         best to use an example so load the file named POINTER.C  and
  10.         display  it on your monitor for an example of a program with
  11.         some pointers in it.
  12.  
  13.              For  the moment,  ignore the data declaration statement
  14.         where we define "index" and two other fields beginning  with
  15.         a star.   It is properly called an asterisk, but for reasons
  16.         we  will see later,  let's agree to call it a star.   If you
  17.         observe  the  first statement,  it should be clear  that  we
  18.         assign the value of 39 to the variable "index".   This is no
  19.         surprise,  we  have been doing it for several programs  now.
  20.         The  next  statement  however,  says to assign  to  "pt1"  a
  21.         strange looking value,  namely the variable "index" with  an
  22.         ampersand in front of it.   In this example, pt1 and pt2 are
  23.         pointers,  and  the  variable "index" is a simple  variable.
  24.         Now we have a problem.  We need to learn how to use pointers
  25.         in a program, but to do so requires that first we define the
  26.         means of using the pointers in the program.
  27.  
  28.              The  following two rules will be somewhat confusing  to
  29.         you at first but we need to state the definitions before  we
  30.         can  use  them.   Take your time,  and the whole thing  will
  31.         clear up very quickly.
  32.  
  33.                           TWO VERY IMPORTANT RULES
  34.  
  35.              The  following two rules are very important when  using
  36.         pointers and must be thoroughly understood.
  37.  
  38.         1.  A variable name with an ampersand in front of it defines
  39.             the  address of the variable and therefore points to the
  40.             variable.  You can therefore read line seven as "pt1  is
  41.             assigned the value of the address of index".
  42.  
  43.         2.  A  pointer  with a "star" in front of it refers  to  the
  44.             value of the variable pointed to by the  pointer.   Line
  45.             ten  of the program can be read as "The stored (starred)
  46.             value  to which the pointer "pt1" points is assigned the
  47.             value  13".   Now  you can see why it is  convenient  to
  48.             think of the asterisk as a star,  it sort of sounds like
  49.             the word store.
  50.  
  51.                                   MEMORY AIDS
  52.  
  53.             1. Think of & as an address.
  54.             2. Think of * as a star referring to stored.
  55.  
  56.  
  57.                                   Page 52
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                             Chapter 8 - Pointers
  68.  
  69.  
  70.  
  71.              Assume for the moment that "pt1" and "pt2" are pointers
  72.         (we will see how to define them shortly).  As pointers, they
  73.         do not contain a variable value but an address of a variable
  74.         and  can  be  used to point to a variable.  Line  7  of  the
  75.         program  assigns the pointer "pt1" to point to the  variable
  76.         we have already defined as "index" because we have  assigned
  77.         the address of "index" to "pt1".  Since we have a pointer to
  78.         "index",  we  can manipulate the value of "index"  by  using
  79.         either the variable name itself, or the pointer.
  80.  
  81.              Line 10 modifies the value by using the pointer.  Since
  82.         the  pointer  "pt1"  points to the  variable  "index",  then
  83.         putting  a star in front of the pointer name refers  to  the
  84.         memory location to which it is pointing.  Line 10  therefore
  85.         assigns to "index" the value of 13. Anyplace in the  program
  86.         where it is permissible to use the variable name "index", it
  87.         is  also permissible to use the name "*pt1" since  they  are
  88.         identical in meaning until the pointer is reassigned to some
  89.         other variable.
  90.  
  91.                               ANOTHER POINTER
  92.  
  93.              Just  to add a little intrigue to the system,  we  have
  94.         another  pointer  defined in this  program,  "pt2".    Since
  95.         "pt2" has not been assigned a value prior to statement 8, it
  96.         doesn't point to anything, it contains garbage.  Of  course,
  97.         that is also true of any variable until a value is  assigned
  98.         to it. Statement 8 assigns "pt2" the same address as  "pt1",
  99.         so  that now "pt2" also points to the variable "index".   So
  100.         to continue the definition from the last paragraph, anyplace
  101.         in  the program where it is permissible to use the  variable
  102.         "index",  it  is  also permissible to use  the  name  "*pt2"
  103.         because  they   are  identical in  meaning.   This  fact  is
  104.         illustrated  in  the  first "printf"  statement  since  this
  105.         statement  uses  the  three means of  identifying  the  same
  106.         variable to print out the same variable three times.
  107.  
  108.                          THERE IS ONLY ONE VARIABLE
  109.  
  110.              Note carefully that,  even though it appears that there
  111.         are three variables, there is really only one variable.  The
  112.         two  pointers  point  to  the  single  variable.    This  is
  113.         illustrated in the next statement which assigns the value of
  114.         13  to  the  variable "index",  because that  is  where  the
  115.         pointer  "pt1"  is pointing.   The next  "printf"  statement
  116.         causes  the  new value of 13 to be printed out three  times.
  117.         Keep  in mind that there is really only one variable  to  be
  118.         changed, not three.
  119.  
  120.  
  121.  
  122.  
  123.                                   Page 53
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                             Chapter 8 - Pointers
  134.  
  135.  
  136.              This is admittedly a very difficult concept,  but since
  137.         it  is  used  extensively  in all but  the  most  trivial  C
  138.         programs,  it  is  well  worth your time to stay  with  this
  139.         material until you understand it thoroughly.
  140.  
  141.                         HOW DO YOU DECLARE A POINTER?
  142.  
  143.              Now  to  keep a promise and tell you how to  declare  a
  144.         pointer.   Refer  to the third line of the program  and  you
  145.         will  see  our  old familiar way of  defining  the  variable
  146.         "index",  followed  by  two more  definitions.   The  second
  147.         definition  can  be read as "the storage location  to  which
  148.         "pt1"  points  will be an int  type  variable".   Therefore,
  149.         "pt1" is a pointer to an int type variable.  Likewise, "pt2"
  150.         is another pointer to an int type variable.
  151.  
  152.              A  pointer  must  be defined to point to some  type  of
  153.         variable.   Following a proper definition, it cannot be used
  154.         to point to any other type of variable or it will result  in
  155.         a  "type incompatibility" error.   In the same manner that a
  156.         "float"  type of variable cannot be added to an  "int"  type
  157.         variable,  a pointer to a "float" variable cannot be used to
  158.         point to an integer variable.
  159.  
  160.              Compile and run this program and observe that there  is
  161.         only  one  variable  and the single  statement  in  line  10
  162.         changes the one variable which is displayed three times.
  163.  
  164.                       THE SECOND PROGRAM WITH POINTERS
  165.  
  166.              In these few pages so far on pointers,  we have covered
  167.         a lot of territory, but it is important territory.  We still
  168.         have  a  lot  of  material to cover so stay in  tune  as  we
  169.         continue  this important aspect of C.   Load the  next  file
  170.         named  POINTER2.C  and display it on your monitor so we  can
  171.         continue our study.
  172.  
  173.              In  this program we have defined several variables  and
  174.         two pointers.   The first pointer named "there" is a pointer
  175.         to  a "char" type variable and the second named "pt"  points
  176.         to an "int" type variable.  Notice also that we have defined
  177.         two  array variables named "strg" and "list".   We will  use
  178.         them  to show the correspondence between pointers and  array
  179.         names.
  180.  
  181.                   A STRING VARIABLE IS ACTUALLY A POINTER
  182.  
  183.              In  the programming language C,  a string  variable  is
  184.         defined to be simply a pointer to the beginning of a string.
  185.         This  will  take  some explaining.   Refer  to  the  example
  186.         program  on  your monitor.   You will notice that  first  we
  187.  
  188.  
  189.                                   Page 54
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                             Chapter 8 - Pointers
  200.  
  201.  
  202.         assign a string constant to the string variable named "strg"
  203.         so we will have some data to work with.  Next, we assign the
  204.         value  of the first element to the variable "one",  a simple
  205.         "char" variable.   Next,  since the string name is a pointer
  206.         by  definition  of the C language,  we can assign  the  same
  207.         value to "two" by using the star and the string  name.   The
  208.         result  of  the two assignments are such that "one" now  has
  209.         the same value as "two", and both contain the character "T",
  210.         the  first character in the string.   Note that it would  be
  211.         incorrect  to  write  the ninth line as  "two  =  *strg[0];"
  212.         because the star takes the place of the square brackets.
  213.  
  214.              For all practical purposes,  "strg" is a  pointer.   It
  215.         does, however, have one restriction that a true pointer does
  216.         not  have.   It cannot be changed like a variable,  but must
  217.         always contain the initial value and therefore always points
  218.         to  its  string.   It  could  be thought  of  as  a  pointer
  219.         constant,  and in some applications you may desire a pointer
  220.         that cannot be corrupted in any way.   Even though it cannot
  221.         be changed, it can be used to refer to other values than the
  222.         one  it is defined to point to,  as we will see in the  next
  223.         section of the program.
  224.  
  225.              Moving ahead to line 13, the variable "one" is assigned
  226.         the  value of the ninth variable (since the indexing  starts
  227.         at zero) and "two" is assigned the same value because we are
  228.         allowed to index a pointer to get to values farther ahead in
  229.         the string.  Both variables now contain the character "a".
  230.  
  231.             The C programming language takes care of indexing for us
  232.         automatically  by  adjusting the indexing for  the  type  of
  233.         variable  the  pointer is pointing to.   In this  case,  the
  234.         index  of  8  is simply added to the  pointer  value  before
  235.         looking up the desired result because a "char" type variable
  236.         is  one byte long.   If we were using a pointer to an  "int"
  237.         type variable,  the index would be doubled and added to  the
  238.         pointer  before  looking up the value because an "int"  type
  239.         variable  uses two bytes per value stored.   When we get  to
  240.         the chapter on structures,  we will see that a variable  can
  241.         have  many,   even  into  the  hundreds  or  thousands,   of
  242.         bytes  per  variable,  but  the  indexing  will  be  handled
  243.         automatically for us by the system.
  244.  
  245.              Since "there" is already a pointer, it can be  assigned
  246.         the  address  of  the  eleventh element  of  "strg"  by  the
  247.         statement  in line 17 of the program.  Remember  that  since
  248.         "there"  is a true pointer, it can be assigned any value  as
  249.         long as that value represents a "char" type of address.   It
  250.         should  be clear that the pointers must be "typed" in  order
  251.         to  allow  the  pointer arithmetic  described  in  the  last
  252.  
  253.  
  254.  
  255.                                   Page 55
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                             Chapter 8 - Pointers
  266.  
  267.  
  268.         paragraph to be done properly.  The third and fourth outputs
  269.         will be the same, namely the letter "c".
  270.  
  271.                              POINTER ARITHMETIC
  272.  
  273.              Not  all  forms  of  arithmetic are  permissible  on  a
  274.         pointer.   Only  those things that make  sense,  considering
  275.         that a pointer is an address somewhere in the computer.   It
  276.         would  make sense to add a constant to an  address,  thereby
  277.         moving it ahead in memory that number of places.   Likewise,
  278.         subtraction  is permissible,  moving it back some number  of
  279.         locations.   Adding  two  pointers together would  not  make
  280.         sense  because absolute memory addresses are  not  additive.
  281.         Pointer multiplication is also not allowed, as that would be
  282.         a  funny number.   If you think about what you are  actually
  283.         doing,  it will make sense to you what is allowed,  and what
  284.         is not.
  285.  
  286.                          NOW FOR AN INTEGER POINTER
  287.  
  288.              The  array named "list" is assigned a series of  values
  289.         from  100  to 199 in order to have some data to  work  with.
  290.         Next  we  assign the pointer "pt" the address  of  the  28th
  291.         element  of the list and print out the same value both  ways
  292.         to  illustrate that the system truly will adjust  the  index
  293.         for the "int" type variable.  You should spend some time  in
  294.         this program until you feel you fairly well understand these
  295.         lessons on pointers.
  296.  
  297.              Compile and run POINTER2.C and study the output.
  298.  
  299.              You  may recall that back in the lesson on functions we
  300.         mentioned that there were two ways to get variable data back
  301.         from a function.   One way is through use of the array,  and
  302.         you should be right on the verge of guessing the other  way.
  303.         If your guess is through use of a pointer,  you are correct.
  304.         Load  and display the program named TWOWAY.C for an  example
  305.         of this.
  306.  
  307.                     FUNCTION DATA RETURN WITH A POINTER
  308.  
  309.              In  TWOWAY.C,  there  are two variables defined in  the
  310.         main program "pecans" and "apples".   Notice that neither of
  311.         these is defined as a pointer.   We assign values to both of
  312.         these  and print them out,  then call the  function  "fixup"
  313.         taking with us both of these values.   The variable "pecans"
  314.         is  simply  sent  to the function,  but the address  of  the
  315.         variable  "apples" is sent to the function.   Now we have  a
  316.         problem.   The two arguments are not the same, the second is
  317.         a pointer to a variable.  We must somehow alert the function
  318.         to  the  fact  that it is supposed  to  receive  an  integer
  319.  
  320.  
  321.                                   Page 56
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.                             Chapter 8 - Pointers
  332.  
  333.  
  334.         variable  and a pointer to an integer variable.   This turns
  335.         out   to  be  very  simple.    Notice  that  the   parameter
  336.         definitions in the function define "nuts" as an integer, and
  337.         "fruit"  as a pointer to an integer.   The call in the  main
  338.         program  therefore  is now in agreement  with  the  function
  339.         heading and the program interface will work just fine.
  340.  
  341.              In  the body of the function,  we print the two  values
  342.         sent  to  the function,  then modify them and print the  new
  343.         values out.   This should be perfectly clear to you by  now.
  344.         The  surprise occurs when we return to the main program  and
  345.         print out the two values again.  We will find that the value
  346.         of  pecans will be restored to its value before the function
  347.         call  because  the C language makes a copy of  the  item  in
  348.         question and takes the copy to the called function,  leaving
  349.         the original intact.   In the case of the variable "apples",
  350.         we  made  a copy of a pointer to the variable and  took  the
  351.         copy of the pointer to the function.  Since we had a pointer
  352.         to  the  original variable,  even though the pointer  was  a
  353.         copy,  we  had  access  to the original variable  and  could
  354.         change  it in the function.   When we returned to  the  main
  355.         program,  we  found  a  changed value in  "apples"  when  we
  356.         printed it out.
  357.  
  358.              By  using  a pointer in a function call,  we  can  have
  359.         access  to the data in the function and change it in such  a
  360.         way  that when we return to the calling program,  we have  a
  361.         changed  value  of data.    It must be pointed out  however,
  362.         that  if you modify the value of the pointer itself  in  the
  363.         function,  you  will have a restored pointer when you return
  364.         because the pointer you use in the function is a copy of the
  365.         original.  In this example, there was no pointer in the main
  366.         program because we simply sent the address to the  function,
  367.         but  in  many  programs you will use  pointers  in  function
  368.         calls.  One of the places you will find need for pointers in
  369.         function  calls  will be when you request data  input  using
  370.         standard  input/output routines.   These will be covered  in
  371.         the next two chapters.
  372.  
  373.              Compile and run TWOWAY.C and observe the output.
  374.  
  375.                            POINTERS ARE VALUABLE
  376.  
  377.              Even  though  you are probably somewhat intimidated  at
  378.         this point by the use of pointers,  you will find that after
  379.         you  gain experience,  you will use them profusely  in  many
  380.         ways.  You will also use pointers in every program you write
  381.         other than the most trivial because they are so useful.  You
  382.         should  probably  go  over this material  carefully  several
  383.         times until you feel comfortable with it because it is  very
  384.  
  385.  
  386.  
  387.                                   Page 57
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.                             Chapter 8 - Pointers
  398.  
  399.  
  400.         important  in the area of input/output which is next on  the
  401.         agenda.
  402.  
  403.  
  404.         PROGRAMMING EXERCISES
  405.  
  406.         1.   Define  a character array  and use "strcpy" to  copy  a
  407.              string  into it.  Print the string out by using a  loop
  408.              with  a  pointer to print out one character at a  time.
  409.              Initialize the pointer to the first element and use the
  410.              double  plus  sign  to increment  the  pointer.  Use  a
  411.              separate  integer variable to count the  characters  to
  412.              print.
  413.  
  414.         2.   Modify the program to print out the string backwards by
  415.              pointing to the end and using a decrementing pointer.
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.                                   Page 58
  454.